var videosModel = require("../models/videos");
var usersModel = require("../models/users");
var { find, count } = require("../dao/base.js");

module.exports.getVideoSearch = async (req, res) => {
  let loginUserid = req.query.loginUserid;
  var loginUserLoveVideo = await find(usersModel, { userid: loginUserid }, [
    "likevideos",
    "follow",
  ]);
  var data = {
    curPage: page,
    pageSize: 9,
  };
  var page = req.query.page || 1;
  let regexp = new RegExp(req.params.key, "g");
  const total = await count(
    videosModel,  
    {
      $or: [
        { introduce: { $regex: regexp } },
        { position: { $regex: regexp } },
        { choosenVideoLabel: { $regex: regexp } },
        { userid: { $regex: regexp } },
      ],
      isEffective: true,
    },
    null
  );
  var result2 = await find(
    videosModel,
    {
      $or: [
        { introduce: { $regex: regexp } },
        { position: { $regex: regexp } },
        { choosenVideoLabel: { $regex: regexp } },
        { userid: { $regex: regexp } },
      ],
      isEffective: true,
    },
    null,
    {
      skip: (page - 1) * data.pageSize,
      limit: data.pageSize,
      sort: { _id: -1 },
    }
  );
  if (loginUserid === "admin") return res.sendResult(result2, 200, total);
  var video = new Array();
  if (loginUserid === undefined) {
    for (let i = 0; i < result2.length; i++) {
      //查找视频作者的信息
      const res = await find(
        usersModel,
        { userid: result2[i].userid, isEffective: true },
        ["photo", "name", "likevideos"]
      );
      result2[i].photo = res[0].photo;
      result2[i].name = res[0].name;
      video.push({
        ...result2[i]._doc,
        name: res[0].name,
        photo: res[0].photo,
        isLike: false,
        isFollow: false,
      });
    }
  } else {
    var video = new Array();
    for (let i = 0; i < result2.length; i++) {
      //查找视频作者的信息
      const res = await find(
        usersModel,
        { userid: result2[i].userid, isEffective: true },
        ["photo", "name", "likevideos"]
      );
      //查找登录用户喜欢的列表是否等于这个视频
      var index =
        loginUserLoveVideo[0].likevideos.findIndex((v) => result2[i]._id == v) +
        1;
      var index2 =
        loginUserLoveVideo[0].follow.findIndex((v) => result2[i].userid == v) +
        1;
      result2[i].photo = res[0].photo;
      result2[i].name = res[0].name;
      video.push({
        ...result2[i]._doc,
        name: res[0].name,
        photo: res[0].photo,
        isLike: index ? true : false,
        isFollow: index2 ? true : false,
      });
    }
  }
  if (video) {
    return res.sendResult(video, 200, total);
  } else {
    return res.sendResult([], 200, total);
  }
};

module.exports.getUserSearch = async (req, res) => {
  var data = {
    curPage: page,
    pageSize: 6,
  };

  var page = req.query.page || 1;
  let regexp = new RegExp(req.params.key, "g");
  const total = await count(
    usersModel,
    {
      $or: [{ name: { $regex: regexp } }, { userid: { $regex: regexp } }],
      isEffective: true,
    },
    null
  );
  var docs = await find(
    usersModel,
    {
      $or: [{ name: { $regex: regexp } }, { userid: { $regex: regexp } }],
      isEffective: true,
    },
    null,
    {
      skip: (page - 1) * data.pageSize,
      limit: data.pageSize,
      sort: { _id: -1 },
    }
  );
  if (docs) {
    return res.sendResult(docs, 200, total);
  }
};
